package ar.edu.uba.fi.jmit.monitorengine.parser;

import java.io.FileReader;
import java.io.IOException;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

import ar.edu.uba.fi.jmit.monitorengine.core.ConfigurationManager;

public class XMLDeleteFileParser extends DefaultHandler {

	//Names of the tags
	private static final String MONITOR_TAG = "monitor";
	private static final String NAME_MONITOR_TAG = "name";
	private static final String VISUALIZER_TAG = "visualizer";
	private static final String NAME_VISUALIZER_TAG = "name";
	
	
	private XMLDeleteFileParser() {
		
	}
	
	public static void parse(String pathToFile) throws SAXException, IOException{
		XMLReader xr = XMLReaderFactory.createXMLReader();
		XMLDeleteFileParser handler = new XMLDeleteFileParser();
		xr.setContentHandler(handler);
		xr.setErrorHandler(handler);
		FileReader r = new FileReader(pathToFile);
		xr.parse(new InputSource(r));
	}
	
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if (qName == MONITOR_TAG){
			if (attributes.getIndex(NAME_MONITOR_TAG) != -1) {
				String monitorName = attributes.getValue(NAME_MONITOR_TAG);
				ConfigurationManager.getInstance().deleteConfiguration(monitorName);
				ConfigurationManager.getInstance().deleteRangeObservers(monitorName);
			}
		}else
		if (qName == VISUALIZER_TAG){
			if (attributes.getIndex(NAME_VISUALIZER_TAG) != -1) {
				String visualizerName = attributes.getValue(NAME_VISUALIZER_TAG);
				ConfigurationManager.getInstance().deleteVisualizer(visualizerName);
			}
		}
	}
}
